package com.wwp.tbshop.view.adapter;

import android.content.Context;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.wwp.tbshop.R;
import com.wwp.tbshop.model.bean.CategoryTempBean;
import com.wwp.tbshop.model.bean.FilterBean;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by 楠GG on 2017/6/20.
 */

public class FilterRvAdapter extends RecyclerView.Adapter {
    private Context mContext;
    private List<FilterBean> mDatas = new ArrayList<>();

    public static final int TYPE_TITLE = 0;
    public static final int TYPE_NORMAL = 1;
    public List<String> mIdList = new ArrayList<>();

    public FilterRvAdapter(Context context) {
        mContext = context;
    }

    public void setDatas(List<FilterBean> datas) {
        this.mDatas = datas;
        notifyDataSetChanged();
    }

    @Override
    public int getItemViewType(int position) {
        FilterBean info = mDatas.get(position);
        int type = info.type;
        if (type == CategoryTempBean.TYPE_TITLE) {
            return TYPE_TITLE;
        } else {
            return TYPE_NORMAL;
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == TYPE_TITLE) {
            View titleView = LayoutInflater.from(mContext).inflate(R.layout.item_filter_title, parent, false);
            FilterTitleHolder titleHolder = new FilterTitleHolder(titleView);
            return titleHolder;
        } else {
            View itemView = LayoutInflater.from(mContext).inflate(R.layout.item_filter, parent, false);
            FilterHolder categoryHolder = new FilterHolder(itemView);
            return categoryHolder;
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        FilterBean filterBean = mDatas.get(position);
        if(filterBean.type == TYPE_TITLE) {
            FilterTitleHolder titleHolder = (FilterTitleHolder) holder;
            titleHolder.setData(mDatas.get(position));
        }else {
            FilterHolder filterHolder = (FilterHolder) holder;
            filterHolder.setData(mDatas.get(position));
        }
    }

    @Override
    public int getItemCount() {
        return mDatas.size();
    }

    class FilterTitleHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.tv_title)
        TextView mTvTitle;

        FilterTitleHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }

        public void setData(FilterBean filterBean) {
            mTvTitle.setText(filterBean.title);
        }
    }

    class FilterHolder extends RecyclerView.ViewHolder{
        @Bind(R.id.tv_name1)
        TextView mTvName1;
        @Bind(R.id.tv_name2)
        TextView mTvName2;
        @Bind(R.id.tv_name3)
        TextView mTvName3;
        private FilterBean mFilterBean;
        private boolean isSelect;

        FilterHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }

        public void setData(FilterBean filterBean) {
            mFilterBean = filterBean;
            mTvName1.setText(filterBean.name1);
            mTvName2.setText(filterBean.name2);
            mTvName3.setText(filterBean.name3);

            mTvName1.setTextColor(Color.GRAY);
            mTvName1.setBackgroundResource(R.drawable.item_filter_normal_shape);
            mTvName2.setTextColor(Color.GRAY);
            mTvName2.setBackgroundResource(R.drawable.item_filter_normal_shape);
            mTvName3.setTextColor(Color.GRAY);
            mTvName3.setBackgroundResource(R.drawable.item_filter_normal_shape);
        }

        @OnClick({R.id.tv_name1, R.id.tv_name2, R.id.tv_name3})
        public void onViewClicked(View view) {
            switch (view.getId()) {
                case R.id.tv_name1:
                    if(isSelect) {
                        mTvName1.setTextColor(Color.GRAY);
                        mTvName1.setBackgroundResource(R.drawable.item_filter_normal_shape);
                        mIdList.clear();
                    }else {
                        String id1 = mFilterBean.id1;
                        mIdList.add(id1);

                        mTvName1.setTextColor(Color.RED);
                        mTvName1.setBackgroundResource(R.drawable.item_filter_select_shape);
                        mTvName2.setTextColor(Color.GRAY);
                        mTvName2.setBackgroundResource(R.drawable.item_filter_normal_shape);
                        mTvName3.setTextColor(Color.GRAY);
                        mTvName3.setBackgroundResource(R.drawable.item_filter_normal_shape);
                    }
                    isSelect = !isSelect;
                    break;
                case R.id.tv_name2:

                    if(isSelect) {
                        mTvName2.setTextColor(Color.GRAY);
                        mTvName2.setBackgroundResource(R.drawable.item_filter_normal_shape);
                        mIdList.clear();
                    }else {
                        String id2 = mFilterBean.id2;
                        mIdList.add(id2);

                        mTvName2.setTextColor(Color.RED);
                        mTvName2.setBackgroundResource(R.drawable.item_filter_select_shape);
                        mTvName1.setTextColor(Color.GRAY);
                        mTvName1.setBackgroundResource(R.drawable.item_filter_normal_shape);
                        mTvName3.setTextColor(Color.GRAY);
                        mTvName3.setBackgroundResource(R.drawable.item_filter_normal_shape);
                    }
                    isSelect = !isSelect;
                    break;
                case R.id.tv_name3:
                    if(isSelect) {
                        mTvName3.setTextColor(Color.GRAY);
                        mTvName3.setBackgroundResource(R.drawable.item_filter_normal_shape);
                        mIdList.clear();
                    }else {
                        String id3 = mFilterBean.id3;
                        mIdList.add(id3);

                        mTvName3.setTextColor(Color.RED);
                        mTvName3.setBackgroundResource(R.drawable.item_filter_select_shape);
                        mTvName2.setTextColor(Color.GRAY);
                        mTvName2.setBackgroundResource(R.drawable.item_filter_normal_shape);
                        mTvName1.setTextColor(Color.GRAY);
                        mTvName1.setBackgroundResource(R.drawable.item_filter_normal_shape);
                    }
                    isSelect = !isSelect;
                    break;
            }
        }

    }
}
